<?php
/**
 * Description:
 *
 * @author      Eden
 * @datetime    2017/2/18 17:19
 * @copyright   WuHan HuQi Technology Co.,Ltd.
 * @url         http://cnhuqi.com
 */


class Cashinsets extends ADM_Controller
{

    const PAGE_SIZE = 20;

    protected $error = '';

    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $this->load->view('admin/money/cashin_sets.html');
    }

    /**
     * 列表
     */
    public function getMeals()
    {
        $send['state'] = false;
        do {

            $page = $this->input->get('page');
            $page = !empty($page) && $page > 1 ? intval($page) : 1;
            $offset = ($page - 1) * self::PAGE_SIZE;

            $this->load->model('common/CashMeal_Model', 'cash');
            $meals = $this->cash->getMeals($offset, self::PAGE_SIZE);
            $total = 0;
            if (!empty($meals)) {
                $total = $this->cash->getTotal();
            }

            $data = [
                'total' => $total,
                'data' => $meals,
                'nextPage' => ($offset + self::PAGE_SIZE) < $total
            ];

            $send['state'] = true;
            $send['data'] = $data;

            $this->output->send($send);

        } while(false);
        $this->output->send($send);
    }

    /**
     * 修改状态，启用、禁用、删除
     */
    public function changeStatus()
    {
        $send['state'] = false;
        do {

            $post = $this->input->post();
            if (empty($post['id']) || empty($post['status'])) {
                $send['error'] = '参数错误';
                break;
            }

            if (!in_array($post['status'], ['on', 'off', 'remove'])) {
                $send['error'] = '参数异常';
                break;
            }

            $this->load->model('common/CashMeal_Model', 'cash');
            if ($this->cash->changeStatus($post)) {
                $send['state'] = true;
            } else {
                $send['error'] = '修改失败';
            }

        } while(false);
        $this->output->send($send);
    }

    /**
     * 修改
     */
    public function doEdit()
    {
        $send['state'] = false;
        do {

            $post = $this->input->post();
            if (empty($post)) {
                $send['error'] = '参数错误';
                break;
            }

            if (!$this->checkParams($post, 'edit')) {
                $send['error'] = '参数错误';
                break;
            }

            $this->load->model('common/CashMeal_Model', 'cash');
            if ($this->cash->modify($post['id'], $post)) {
                $send['state'] = true;
            } else {
                $send['error'] = '修改失败';
            }
        } while(false);
        $this->output->send($send);
    }

    /**
     * 添加
     */
    public function doAdd()
    {
        $send['state'] = false;
        do {

            $post = $this->input->post();
            if (empty($post)) {
                $send['error'] = '参数错误';
                break;
            }

            if (!$this->checkParams($post, 'add')) {
                $send['error'] = '参数错误';
                break;
            }

            $this->load->model('common/CashMeal_Model', 'cash');
            $data = $this->cash->add($post);
            if ($data) {
                $data['amount_format'] = money_format('%i', $data['amount'] / 100);
                $send['state'] = true;
                $send['data'] = $data;
            } else {
                $send['error'] = '添加成功';
            }
        } while(false);
        $this->output->send($send);
    }

    /**
     * Check params
     * @param $posts
     * @param string $type
     * @return bool
     */
    private function checkParams($posts, $type = 'add')
    {
        if (empty($posts['name']) || empty($posts['amount']) || empty($posts['description'])) {
            $this->error = '参数错误';
            return false;
        }

        if ($type == 'edit' && empty($posts['id'])) {
            $this->error = '参数错误';
            return false;
        }

        return true;
    }
}

